﻿using System;
using System;
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Text;
using netDxf;
using netDxf.Blocks;
using netDxf.Collections;
using netDxf.Entities;
using netDxf.Header;
using netDxf.Objects;
using netDxf.Tables;
using netDxf.Units;
using Attribute = netDxf.Entities.Attribute;
using Image = netDxf.Entities.Image;
using Point = netDxf.Entities.Point;
using Trace = netDxf.Entities.Trace;
using System.Threading;

namespace TestDxfDocument
{

    class Toolholderswithrectangularshankfor4
    {
        public static void _Toolholderswithrectangularshankfor4(Dictionary<string, Object> data)//细柄丝锥
        {
            DxfDocument dxf = new DxfDocument();
            //图层
            Layer contour = new Layer("contour") { Color = AciColor.Cyan };    //轮廓层
            Layer dimensioning = new Layer("dimensioning");    //尺寸层
            Layer refin = new Layer("refin") { Color = AciColor.Red };    //基准线/内轮廓
            refin.Linetype = Linetype.Dashed;
            Layer centre = new Layer("centre") { Color = AciColor.Yellow };    //中心线
            Layer textlayer = new Layer("textlayer") { Color = AciColor.Blue };    //文本层
            Layer hatching = new Layer("hatching") { Color = AciColor.Blue };    //填充层
            centre.Linetype = Linetype.DashDot;
            Layer additionalline = new Layer("additionalline") { Color = AciColor.Cyan };    //附加线
            Layer cutting = new Layer("cutting") { Color = AciColor.Red };    //切削层
            Layer nocutting = new Layer("nocutting");    //非切削层
            Layer skcontour = new Layer("skcontour") { Color = AciColor.Cyan };    //sk轮廓层
            Layer skdimensioning = new Layer("skdimensioning");    //sk尺寸线层
            Layer skrefin = new Layer("skrefin") { Color = AciColor.Red };    //sk基准线/内轮廓
            Layer sktext = new Layer("sktext") { Color = AciColor.Blue };  //sk文本
            Layer skhatching = new Layer("skhatching") { Color = AciColor.Blue };    //sk填充
            Layer skadditionlline = new Layer("skadditionlline") { Color = AciColor.Cyan };    //sk附加线
            Layer skviewcontour = new Layer("skviewcontour") { Color = AciColor.Cyan };    //skview轮廓
            Layer skviewdimensioning = new Layer("skviewdimensioning");    //skview尺寸线层

            //参数
            double pi = Math.PI;
            string path = Convert.ToString(data["path"]);

            double scale = 1; //放大系数


            double h1 = 20;
            double b = 20;
            double l3 = 16.5;
            double f = 25;
            double h2 = 20;
            double l1 = 125;
            double l2 = 32;
            double s = 3.97;
            double r = 0.5;
            var a = "0";
            try
            {
                a = data["a"].ToString();
            }
            catch (Exception e)
            {
                Console.WriteLine("[a] not found");
            }

            //坐标
            Vector2 v1 = new Vector2(0, 0);
            Vector2 v2 = new Vector2(v1.X + f / 1.115, v1.Y - l2 / 7.351);
            Vector2 v3 = new Vector2(v1.X + f, v1.Y - l2);
            Vector2 v4 = fun.Polar(v3, 93 * pi / 180, l3);
            Vector2 v5 = new Vector2(v2.X + f / 45.957, v4.Y - (v4.X - v2.X - f / 45.957) * Math.Tan(33 * pi / 180));
            Vector2 v6 = fun.Polar(v3, 153 * pi / 180, l3);
            Vector2 v8 = fun.Polar(v6, 33 * pi / 180, 0.8);
            Vector2 v9 = new Vector2(v1.X, v8.Y + l2 / 5.339);
            Vector2 v10 = new Vector2(v1.X + b, v1.Y);
            Vector2 v11 = new Vector2(v10.X, v10.Y + l2);
            Vector2 v12 = fun.Polar(v11, 162 * pi / 180, b / 6.605);
            Vector2 v13 = fun.Polar(v12, -178 * pi / 180, b / 8.168);
            Vector2 v14 = fun.Polar(v13, -178 * pi / 180, b / 6.772);
            Vector2 v15 = fun.Polar(v14, -179 * pi / 180, b / 2.916);
            Vector2 v16 = new Vector2(v1.X, v1.Y + l2 / 0.9);
            Vector2 v17 = new Vector2(v11.X, v11.Y + 0.5);
            Vector2 v18 = new Vector2(v12.X, v12.Y + 0.5);
            Vector2 v19 = new Vector2(v13.X, v13.Y + 0.5);
            Vector2 v20 = new Vector2(v14.X, v14.Y + 0.5);
            Vector2 v21 = new Vector2(v15.X, v15.Y + 0.5);
            Vector2 v22 = new Vector2(v16.X, v16.Y + 0.5);
            Vector2 v23 = new Vector2(v22.X, v22.Y + l1 / 2);
            Vector2 v24 = new Vector2(v17.X, v23.Y);
            Vector2 v25 = new Vector2(v24.X, v24.Y - h2 / 2);
            Vector2 v26 = new Vector2(v25.X, v25.Y - h2);
            Vector2 v27 = new Vector2(v23.X, v26.Y);
            Vector2 v28 = new Vector2(v27.X, v25.Y);



            //作图
            LwPolyline line1 = new LwPolyline();
            line1.Vertexes.Add(new LwPolylineVertex(v5));
            line1.Vertexes.Add(new LwPolylineVertex(v4));
            line1.Vertexes.Add(new LwPolylineVertex(v3));
            line1.Vertexes.Add(new LwPolylineVertex(v6));
            line1.Vertexes.Add(new LwPolylineVertex(v5));
            line1 = Pus.Fillet4LwPline(line1, 1, r);
            line1 = Pus.Fillet4LwPline(line1, 3, r);
            line1 = Pus.Fillet4LwPline(line1, 5, r);
            fun.PolyFill(dxf, line1, 0.05, contour);

            LwPolyline line2 = new LwPolyline();
            line2.Vertexes.Add(new LwPolylineVertex(v5));
            line2.Vertexes.Add(new LwPolylineVertex(v2));
            line2.Vertexes.Add(new LwPolylineVertex(v10));
            line2.Vertexes.Add(new LwPolylineVertex(v1));
            line2.Vertexes.Add(new LwPolylineVertex(v9));
            line2.Vertexes.Add(new LwPolylineVertex(v8));
            fun.PolyFill(dxf, line2, 0.05, contour);

            LwPolyline line3 = new LwPolyline();
            line3.Vertexes.Add(new LwPolylineVertex(v10));
            line3.Vertexes.Add(new LwPolylineVertex(v11));
            line3.Vertexes.Add(new LwPolylineVertex(v12));
            line3.Vertexes.Add(new LwPolylineVertex(v13));
            line3.Vertexes.Add(new LwPolylineVertex(v14));
            line3.Vertexes.Add(new LwPolylineVertex(v15));
            line3.Vertexes.Add(new LwPolylineVertex(v16));
            line3.Vertexes.Add(new LwPolylineVertex(v1));
            line3.Vertexes[1].Bulge = Math.Tan(-45 * pi / 180 / 4);
            line3.Vertexes[2].Bulge = Math.Tan(89 * pi / 180 / 4);
            line3.Vertexes[3].Bulge = Math.Tan(-80 * pi / 180 / 4);
            line3.Vertexes[4].Bulge = Math.Tan(16 * pi / 180 / 4);
            line3.Vertexes[5].Bulge = Math.Tan(-36 * pi / 180 / 4);
            fun.PolyFill(dxf, line3, 0.05, contour);

            LwPolyline line4 = new LwPolyline();
            line4.Vertexes.Add(new LwPolylineVertex(v26));
            line4.Vertexes.Add(new LwPolylineVertex(v17));
            line4.Vertexes.Add(new LwPolylineVertex(v18));
            line4.Vertexes.Add(new LwPolylineVertex(v19));
            line4.Vertexes.Add(new LwPolylineVertex(v20));
            line4.Vertexes.Add(new LwPolylineVertex(v21));
            line4.Vertexes.Add(new LwPolylineVertex(v22));
            line4.Vertexes.Add(new LwPolylineVertex(v23));
            line4.Vertexes.Add(new LwPolylineVertex(v24));
            line4.Vertexes.Add(new LwPolylineVertex(v26));
            line4.Vertexes.Add(new LwPolylineVertex(v27));
            line4.Vertexes.Add(new LwPolylineVertex(v28));
            line4.Vertexes.Add(new LwPolylineVertex(v25));
            line4.Vertexes[1].Bulge = Math.Tan(-45 * pi / 180 / 4);
            line4.Vertexes[2].Bulge = Math.Tan(89 * pi / 180 / 4);
            line4.Vertexes[3].Bulge = Math.Tan(-80 * pi / 180 / 4);
            line4.Vertexes[4].Bulge = Math.Tan(16 * pi / 180 / 4);
            line4.Vertexes[5].Bulge = Math.Tan(-36 * pi / 180 / 4);
            fun.PolyFill(dxf, line4, 0.05, contour);


            //填充
            LwPolyline hatchpoly1 = new LwPolyline();
            hatchpoly1.Vertexes.Add(new LwPolylineVertex(v28));
            hatchpoly1.Vertexes.Add(new LwPolylineVertex(v25));
            hatchpoly1.Vertexes.Add(new LwPolylineVertex(v26));
            hatchpoly1.Vertexes.Add(new LwPolylineVertex(v27));
            hatchpoly1.Vertexes.Add(new LwPolylineVertex(v28));
            hatchpoly1.IsClosed = true;
            String[] htype1 = new String[100];
            Line[] hline1 = new Line[100];
            Arc[] harc1 = new Arc[100];
            int hnumst1 = hatchpoly1.Explode().Count;  //记录爆炸后，线段有多少
            for (int i = 0; i < hnumst1; i++)
            {
                htype1[i] = hatchpoly1.Explode()[i].Type.ToString();    //将所有的元素记录成一个数组
                if (htype1[i] == "Line")
                {
                    hline1[i] = (Line)hatchpoly1.Explode()[i];
                }
                if (htype1[i] == "Arc")
                {
                    harc1[i] = (Arc)hatchpoly1.Explode()[i];
                }
            }
            fun.hatchsub1(dxf, htype1, hnumst1, 45, 10, hline1, harc1, hatching);


            //标注

            double height = 2;
            double arrowsize = 1;
            double exline = 2;




            fun.dimrot1(dxf, v25, v26, -90, f / 2, 1, height, arrowsize, exline, "h2=" + h2, "", "");

            fun.dimrot1(dxf, v2, v3, -90, f, 1, height, arrowsize, exline, "l2=" + l2, "", "");

            fun.dimrot1(dxf, v24, v3, -90, f * 3 / 2, 1, height, arrowsize, exline, "l1=" + l1, "", "");

            fun.dimrot1(dxf, v24, v23, 0, f / 2, 1, height, arrowsize, exline, "b=" + b, "", "");

            fun.dimrot1(dxf, v6, v4, 33, f / 2, 1, height, arrowsize, exline, "l3=" + l3, "", "");

            fun.dimrot1(dxf, v9, v3, 180, f, 1, height, arrowsize, exline, "f=" + f, "", "");

            fun.jdbz2(dxf, new Line(v3, fun.Polar(v3, 0 * pi / 180, 0.1)), new Line(v3, fun.Polar(v3, 93 * pi / 180, 0.1)), f / 2, height, arrowsize, exline, "93°", "", "");

            fun.jdbz2(dxf, new Line(v3, fun.Polar(v3, 93 * pi / 180, 0.1)), new Line(v3, fun.Polar(v3, 148 * pi / 180, 0.1)), l3 / 2, height, arrowsize, exline, "55°", "", "");


            if (a.Equals("7"))
            {

                Vector2 v29 = new Vector2(v1.X - f * 2, v1.Y);
                Vector2 v30 = fun.Polar(v29, -97 * pi / 180, h1 / Math.Cos(7 * pi / 180));
                Vector2 v31 = new Vector2(v29.X - f, v30.Y);
                Vector2 v32 = new Vector2(v31.X, v29.Y);
                Vector2 v33 = new Vector2(v32.X + f - l3 * Math.Cos(33 * pi / 18), v29.Y);
                Vector2 v34 = new Vector2(v33.X + f / 25.933, v29.Y - s);
                Vector2 v35 = new Vector2(v34.X ,v30.Y);
                Vector2 v36 = new Vector2(v29.X - s * Math.Tan(7 * pi / 180), v34.Y);


                LwPolyline line5 = new LwPolyline();
                line5.Vertexes.Add(new LwPolylineVertex(v33));
                line5.Vertexes.Add(new LwPolylineVertex(v34));
                line5.Vertexes.Add(new LwPolylineVertex(v36));
                line5.Vertexes.Add(new LwPolylineVertex(v29));
                line5.Vertexes.Add(new LwPolylineVertex(v32));
                line5.Vertexes.Add(new LwPolylineVertex(v31));
                line5.Vertexes.Add(new LwPolylineVertex(v30));
                line5.Vertexes.Add(new LwPolylineVertex(v29));
                fun.PolyFill(dxf, line5, 0.05, contour);

                LwPolyline line6 = new LwPolyline();
                line6.Vertexes.Add(new LwPolylineVertex(v35));
                line6.Vertexes.Add(new LwPolylineVertex(v34));
                fun.PolyFill(dxf, line6, 0.05, contour);


                //填充
                LwPolyline hatchpoly2 = new LwPolyline();
                hatchpoly2.Vertexes.Add(new LwPolylineVertex(v34));
                hatchpoly2.Vertexes.Add(new LwPolylineVertex(v35));
                hatchpoly2.Vertexes.Add(new LwPolylineVertex(v30));
                hatchpoly2.Vertexes.Add(new LwPolylineVertex(v36));
                hatchpoly2.Vertexes.Add(new LwPolylineVertex(v34));
                hatchpoly2.IsClosed = true;
                String[] htype2 = new String[100];
                Line[] hline2 = new Line[100];
                Arc[] harc2 = new Arc[100];
                int hnumst2 = hatchpoly2.Explode().Count;  //记录爆炸后，线段有多
                                                           //
                                                           //少
                for (int i = 0; i < hnumst2; i++)
                {
                    htype2[i] = hatchpoly2.Explode()[i].Type.ToString();    //将所有的元素记录成一个数组
                    if (htype2[i] == "Line")
                    {
                        hline2[i] = (Line)hatchpoly2.Explode()[i];
                    }
                    if (htype2[i] == "Arc")
                    {
                        harc2[i] = (Arc)hatchpoly2.Explode()[i];
                    }
                }
                fun.hatchsub1(dxf, htype2, hnumst2, 45, 10, hline2, harc2, hatching);

                LwPolyline hatchpoly3 = new LwPolyline();
                hatchpoly3.Vertexes.Add(new LwPolylineVertex(v33));
                hatchpoly3.Vertexes.Add(new LwPolylineVertex(v34));
                hatchpoly3.Vertexes.Add(new LwPolylineVertex(v36));
                hatchpoly3.Vertexes.Add(new LwPolylineVertex(v29));
                hatchpoly3.Vertexes.Add(new LwPolylineVertex(v33));
                hatchpoly3.IsClosed = true;
                String[] htype3 = new String[100];
                Line[] hline3 = new Line[100];
                Arc[] harc3 = new Arc[100];
                int hnumst3 = hatchpoly3.Explode().Count;  //记录爆炸后，线段有多
                                                           //
                                                           //少
                for (int i = 0; i < hnumst3; i++)
                {
                    htype3[i] = hatchpoly3.Explode()[i].Type.ToString();    //将所有的元素记录成一个数组
                    if (htype3[i] == "Line")
                    {
                        hline3[i] = (Line)hatchpoly3.Explode()[i];
                    }
                    if (htype3[i] == "Arc")
                    {
                        harc3[i] = (Arc)hatchpoly3.Explode()[i];
                    }
                }
                fun.hatchsub1(dxf, htype3, hnumst3, 135, 10, hline3, harc3, hatching);

                fun.dimrot1(dxf, v29, v30, -90, f, 1, height, arrowsize, exline, "h1=" + h1, "", "");

                fun.dimrot1(dxf, v29, v36, -90, f / 4, 1, height, arrowsize, exline, "s=" + s, "", "");


            }

            if (a.Equals("0"))
            {

                Vector2 v29 = new Vector2(v1.X - f * 2, v1.Y);
                Vector2 v30 = fun.Polar(v29, -97 * pi / 180, h1 / Math.Cos(7 * pi / 180));
                Vector2 v31 = new Vector2(v29.X - f, v30.Y);
                Vector2 v32 = new Vector2(v31.X, v29.Y + f * Math.Tan(6 * pi / 180));
                Vector2 v33 = new Vector2(v32.X + f - l3 * Math.Cos(33 * pi / 18), v29.Y +  l3 * Math.Cos(33 * pi / 18) * Math.Tan(6 * pi / 180));
                Vector2 v34 = fun.Polar(v33, -92 * pi / 180, s);
                Vector2 v35 = new Vector2(v34.X, v30.Y);
                Vector2 v36 = new Vector2(v29.X - s * Math.Tan(7 * pi / 180), v29.Y - (v33.Y - v34.Y));


                LwPolyline line5 = new LwPolyline();
                line5.Vertexes.Add(new LwPolylineVertex(v33));
                line5.Vertexes.Add(new LwPolylineVertex(v34));
                line5.Vertexes.Add(new LwPolylineVertex(v36));
                line5.Vertexes.Add(new LwPolylineVertex(v29));
                line5.Vertexes.Add(new LwPolylineVertex(v32));
                line5.Vertexes.Add(new LwPolylineVertex(v31));
                line5.Vertexes.Add(new LwPolylineVertex(v30));
                line5.Vertexes.Add(new LwPolylineVertex(v29));
                fun.PolyFill(dxf, line5, 0.05, contour);

                LwPolyline line6 = new LwPolyline();
                line6.Vertexes.Add(new LwPolylineVertex(v35));
                line6.Vertexes.Add(new LwPolylineVertex(v34));
                fun.PolyFill(dxf, line6, 0.05, contour);


                //填充
                LwPolyline hatchpoly2 = new LwPolyline();
                hatchpoly2.Vertexes.Add(new LwPolylineVertex(v34));
                hatchpoly2.Vertexes.Add(new LwPolylineVertex(v35));
                hatchpoly2.Vertexes.Add(new LwPolylineVertex(v30));
                hatchpoly2.Vertexes.Add(new LwPolylineVertex(v36));
                hatchpoly2.Vertexes.Add(new LwPolylineVertex(v34));
                hatchpoly2.IsClosed = true;
                String[] htype2 = new String[100];
                Line[] hline2 = new Line[100];
                Arc[] harc2 = new Arc[100];
                int hnumst2 = hatchpoly2.Explode().Count;  //记录爆炸后，线段有多
                                                           //
                                                           //少
                for (int i = 0; i < hnumst2; i++)
                {
                    htype2[i] = hatchpoly2.Explode()[i].Type.ToString();    //将所有的元素记录成一个数组
                    if (htype2[i] == "Line")
                    {
                        hline2[i] = (Line)hatchpoly2.Explode()[i];
                    }
                    if (htype2[i] == "Arc")
                    {
                        harc2[i] = (Arc)hatchpoly2.Explode()[i];
                    }
                }
                fun.hatchsub1(dxf, htype2, hnumst2, 45, 10, hline2, harc2, hatching);

                LwPolyline hatchpoly3 = new LwPolyline();
                hatchpoly3.Vertexes.Add(new LwPolylineVertex(v33));
                hatchpoly3.Vertexes.Add(new LwPolylineVertex(v34));
                hatchpoly3.Vertexes.Add(new LwPolylineVertex(v36));
                hatchpoly3.Vertexes.Add(new LwPolylineVertex(v29));
                hatchpoly3.Vertexes.Add(new LwPolylineVertex(v33));
                hatchpoly3.IsClosed = true;
                String[] htype3 = new String[100];
                Line[] hline3 = new Line[100];
                Arc[] harc3 = new Arc[100];
                int hnumst3 = hatchpoly3.Explode().Count;  //记录爆炸后，线段有多
                                                           //
                                                           //少
                for (int i = 0; i < hnumst3; i++)
                {
                    htype3[i] = hatchpoly3.Explode()[i].Type.ToString();    //将所有的元素记录成一个数组
                    if (htype3[i] == "Line")
                    {
                        hline3[i] = (Line)hatchpoly3.Explode()[i];
                    }
                    if (htype3[i] == "Arc")
                    {
                        harc3[i] = (Arc)hatchpoly3.Explode()[i];
                    }
                }
                fun.hatchsub1(dxf, htype3, hnumst3, 135, 10, hline3, harc3, hatching);

                fun.dimrot1(dxf, v29, v30, -90, f, 1, height, arrowsize, exline, "h1=" + h1, "", "");

                fun.dimrot1(dxf, v29, v36, -90, f / 4, 1, height, arrowsize, exline, "s=" + s, "", "");

                fun.jdbz2(dxf, new Line(v29, fun.Polar(v29, -6 * pi / 180, 0.1)), new Line(v29, fun.Polar(v29, 0 * pi / 180, 0.1)), f / 2, height, arrowsize, exline, "6°±1°", "", "");


            }




            //定义格式，路径导出
            dxf.DrawingVariables.AcadVer = DxfVersion.AutoCad2007;
            dxf.Save(path);


        }

    }
}
